﻿Shader "Dan/UI/UI_SpotWave"
{
    Properties
    {
       _Color_Inside ("Color_Inside", Color) = (0.07843138,0.3921569,0.7843137,1)
       _Color_Outside ("Color_Outside", Color) = (1,0,0.7320004,1)
       _Emission ("Emission", Float ) = 1
       _Density ("Density", Float ) = 16
       _Speed ("Speed", Float ) = 1
       _Frequency ("Frequency", Float ) = 4
       _SpotSize ("SpotSize", Range(0, 1)) = 0.5
       [MaterialToggle] _UseRadialDirection ("UseRadialDirection", Float ) = 0
       _RadialMask ("RadialMask", Range(0, 1)) = 0.1
       [MaterialToggle] _UseAlpha ("UseAlpha", Float ) = 1

    }
    
    SubShader
    {

        Tags { "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent" }
        LOD 100    
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off 
 
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            #include "../ShaderLibs/UIFX.cginc"
            #include "../ShaderLibs/UVOperator.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            
            struct v2f
            {
                float4 vertex : SV_POSITION;    
                float2 uv : TEXCOORD0;
                
            };

             float4 _Color_Inside;
             float _Density;
             float _Speed;
             float4 _Color_Outside;
             float _SpotSize;
             fixed _UseRadialDirection;
             float _RadialMask;
             float _Emission;
             fixed _UseAlpha;
             float _Frequency;

            v2f vert ( appdata v )
            {
                
                v2f o;
                UNITY_INITIALIZE_OUTPUT(v2f ,o);
                o.vertex = UnityObjectToClipPos(v.vertex);
                
                o.uv = v.uv;        
                return o;
            }
            
            


            fixed4 frag (v2f i ) : SV_Target
            {
                float2 uv = i.uv;
                
                 
                float2 ruv = abs((uv-0.5)*2);//rawuv
                float circle = length(ruv);//求圆
                
                //uv分层 求mask
                float denstiy = _Density*0.5;
                float2 uvlayer =1-floor(ruv * denstiy) / (denstiy - 1);
                float mask =saturate(uvlayer.x*uvlayer.y);
                mask =step(_RadialMask,mask);
                //辐射状 判断是否启用辐射方向
                float radialshape = saturate((0.5 - (ruv.x-0.5)*(ruv.y-0.5)*2));
                float radialDir =lerp( radialshape, circle, _UseRadialDirection );

                //求流动速度
                float sinSpeed =abs(sin(((radialDir*_Frequency)-(_Time.y*_Speed))));
                //求波点
                float spot =length(frac(uv*_Density)*2-1);
                //求波点流动
                float spotFlow=sinSpeed*0.5+0.5-spot;
                //波点运动 大小
                float spotwave = step((1 - _SpotSize),spotFlow)*mask;
                //判断启用alpha 剔除
                float useAlpha =lerp( 1, spotwave, _UseAlpha );
                clip( useAlpha- 0.5);

                float3 finalColor  = lerp(_Color_Inside.rgb,_Color_Outside.rgb,circle)*_Emission*spotwave;
                
                return fixed4(finalColor,1);
                
                
            }
            ENDCG
        }
    }
    
    
    
}
